package com.avfh.netcard.service.impl;

import com.avfh.netcard.common.Result;
import com.avfh.netcard.model.Card;
import com.avfh.netcard.model.User;
import com.avfh.netcard.mapper.UserMapper;
import com.avfh.netcard.service.ICardService;
import com.avfh.netcard.service.IUserService;
import com.avfh.netcard.service.MobileIotService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wuhui
 * @since 2017-06-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    ICardService cardService;

    @Override
    public Result register(User user) {
        EntityWrapper<Card> cardWp = new EntityWrapper<>();
        cardWp.eq(Card.SIM, user.getSim());
        Card card = new Card().selectOne(cardWp);
        if (card == null) {
            return new Result<>("无效的SIM卡或本平台未出售过此SIM卡", "1002");
        }
        User existUser;
        EntityWrapper<User> wrapper;
        wrapper = new EntityWrapper<>();
        wrapper.eq(User.TELEPHONE, user.getTelephone());
        existUser = new User().selectOne(wrapper);
        if (existUser != null) {
            return new Result<>("该手机号已注册", "1002");
        }
        wrapper = new EntityWrapper<>();
        wrapper.eq(User.SIM, user.getSim());
        existUser = new User().selectOne(wrapper);
        if (existUser != null) {
            return new Result<>("该SIM卡已注册", "1002");
        }
        user.setCreatedAt(new Date()).insert();
        return new Result<>("注册成功", "1001");
    }
}
